<!-- <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .box{
      width: 200px;
      height: 200px;
      background-color: pink;
    }
  </style>
</head>
<body>
  <div class="box"></div>
  <script>
    var box = document.querySelector('.box');
    var startX, startY, endX, endY, plane_lengthX,vertical_lengthY
    box.addEventListener('touchstart',function(e){
      startX = e.targetTouches[0].clientX;
      startY = e.targetTouches[0].clientY;
      console.log(startX,startY)
    });
    box.addEventListener('touchend',function(e){
      endX = e.changedTouches[0].clientX;
      endY = e.changedTouches[0].clientY;
      console.log(endX,endY)
      plane_lengthX = Math.abs(endX - startX);
      vertical_lengthY = Math.abs(endY - startY);
      if(plane_lengthX > vertical_lengthY){
        alert('水平移动');
        if(endX > startX){
          console.log('向右移动');
        }else{
          console.log('向左移动');
        }
        if(endY > startY){
          console.log('向下移动');
        }else{
          console.log('向上移动');
        }
      }else{
        alert('垂直移动');
        if(endX > startX){
          console.log('向右移动');
        }else{
          console.log('向左移动');
        }
        if(endY > startY){
          console.log('向下移动');
        }else{
          console.log('向上移动');
        }
      }
    })
  </script>
</body>
</html> -->

<!-- <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .box{
      width: 200px;
      height: 200px;
      background-color: pink;
    }
  </style>
</head>
<body>
  <div class="box"></div>
  <script>
    var box = document.querySelector('.box');
    var startX, startY, endX, endY, plane_lengthX,vertical_lengthY
    box.addEventListener('touchstart',function(e){
      startX = e.targetTouches[0].clientX;
      startY = e.targetTouches[0].clientY;
      console.log(startX,startY)
    });
    box.addEventListener('touchend',function(e){
      endX = e.changedTouches[0].clientX;
      endY = e.changedTouches[0].clientY;
      console.log(endX,endY)
      plane_lengthX = Math.abs(endX - startX);
      vertical_lengthY = Math.abs(endY - startY);
      if(plane_lengthX > vertical_lengthY){
        alert('水平移动');
        if(endX > startX && endY > startY){
          console.log('向右下移动');
        }else if(endX > startX && endY < startY){
          console.log('向右上移动');
        }else if(endX < startX && endY > startY){
          console.log('向左下移动');
        }else{
          console.log('向左上移动');
        }
      }else{
        alert('垂直移动');
        if(endX > startX){
          console.log('向右移动');
        }else{
          console.log('向左移动');
        }
        if(endY > startY){
          console.log('向下移动');
        }else{
          console.log('向上移动');
        }
      }
    })
  </script>
</body>
</html> -->

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .box{
      width: 200px;
      height: 200px;
      background-color: pink;
    }
  </style>
</head>
<body>
  <div class="box"></div>
  <script>
    var box = document.querySelector('.box');
    var startX, startY, endX, endY, plane_lengthX,vertical_lengthY
    box.addEventListener('touchstart',function(e){
      startX = e.targetTouches[0].clientX;
      startY = e.targetTouches[0].clientY;
      console.log(startX,startY);
    });
    box.addEventListener('touchend',function(e){
      endX = e.changedTouches[0].clientX;
      endY = e.changedTouches[0].clientY;
      console.log(endX,endY);
      plane_lengthX = Math.abs(endX - startX);
      vertical_lengthY = Math.abs(endY - startY);
      //方法一
      plane_lengthX > vertical_lengthY ?  endX > startX ? alert('水平向右移动') : alert('水平向左移动') : endY > startY ? alert('垂直向下移动') : alert('垂直向上移动');
      //方法二
      if(plane_lengthX > vertical_lengthY){
        alert('水平移动');
        if(endX > startX && endY > startY){
          console.log('向右下移动');
        }else if(endX > startX && endY < startY){
          console.log('向右上移动');
        }else if(endX < startX && endY > startY){
          console.log('向左下移动');
        }else{
          console.log('向左上移动');
        }
      }else{
        alert('垂直移动');
        if(endX > startX){
          console.log('向右移动');
        }else{
          console.log('向左移动');
        }
        if(endY > startY){
          console.log('向下移动');
        }else{
          console.log('向上移动');
        }
      }
    })
  </script>
</body>
</html>
